package com.hnkywl.sts.dao.cwgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.dao.cwgl.kmys.CyfyysDao;
import com.hnkywl.sts.dao.cwgl.kmys.CykmdmysDao;
import com.hnkywl.sts.entity.cwgl.Khzhsfmx;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.cwgl.kmys.Cyfyys;
import com.hnkywl.sts.entity.cwgl.kmys.Cykmdmys;
import com.hnkywl.sts.entity.cwgl.kmys.Ljkmdmys;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Xyz;
import com.hnkywl.sts.service.cwgl.KmdmService;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-3
 * Time: 下午3:11
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class XyzdrDao extends SimpleHibernateTemplate<Xyz,Long> {
    private KmdmService kmdmService;
    private CykmdmysDao cykmdmysDao;

    /**
     * 查询贴现承兑
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Xyz> findAllXyzdr(String xyzhm,Long gsId,Integer xyzdrZt,Integer voucherNum,String czr,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Xyz> page = new Page<Xyz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (xyzhm != null && !("").equals(xyzhm)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" xyzhm like ? ");
            param.add("%"+xyzhm.trim()+"%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(gsId);
        }
        if (voucherNum != null && ! voucherNum.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" voucherNum = ? ");
            param.add(voucherNum);
        }
        if (czr != null && !czr.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" createUser.xm like ?");
            param.add("%" +czr.trim()+ "%");
        }
        if (xyzdrZt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            //status=0未导入
            //status=1导入成功
            //status=2导入失败
            //status=3删除成功
            //status=4删除失败
            condition.append(" ("+xyzdrZt+"=0 and insertFlag=1) " +
                    "or ("+xyzdrZt+"=1 and insertFlag=3 and deleteFlag!=2) " +
                    "or ("+xyzdrZt+"=2 and insertFlag=2) " +
                    "or ("+xyzdrZt+"=3 and deleteFlag=3 and insertFlag!=2) " +
                    "or ("+xyzdrZt+"=4 and deleteFlag=2) " );
        }
        //==============记录数量===============
        hql = "select count(*) from Xyz where (status in (2,3)) " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Xyz where (status in  (2,3) ) " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Khzhsfmx> getKhhzfmx(Long xyzId){
        String   hql = " from Khzhsfmx where xyz.id = ? ";
        return  find(hql,xyzId);
    }


    /**
     * 通过票据返回凭证部分
     * @param
     * @return
     */
    public List<VoucherItemData> getVoucher(Xyz xyz,Integer xyzdrDrlx) {
        List<VoucherItemData> voucherItemDatas = new ArrayList<VoucherItemData>();
        //信用证利息的导入 新建应付信用证
        if(xyzdrDrlx==1){
            Cykmdmys   cykmdmys1 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"3100");
            String ccname1 =cykmdmys1.getJqcwkmmc();
            String ccode1 =cykmdmys1.getKmdm();
            Double je1 = xyz.getBzj()==null?0:xyz.getBzj();
            String ccode2 =xyz.getYfczyh().getKmdm()+"";//这里应该是开户行的jiuqicode
            String ccname2 ="银行存款/"+xyz.getYfczyh().getQc();
            Double je2 = xyz.getBzj()==null?0:xyz.getBzj();
            Cykmdmys   cykmdmys3 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"3200");
            String ccname3 =cykmdmys3.getJqcwkmmc();
            String ccode3 =cykmdmys3.getKmdm();
            Double je3 = xyz.getKzf();
            String ccode4 =xyz.getYfczyh().getKmdm()+"";//这里应该是开户行的jiuqicode
            String ccname4 ="银行存款/"+xyz.getYfczyh().getQc();
            Double je4 = xyz.getKzf();

            if(je1 !=0){
                VoucherItemData voucherItemData1 = new VoucherItemData();
                voucherItemData1.setZy("转存"+xyz.getYfczyh().getQc()+"保证金");
                voucherItemData1.setKmdm(ccode1);
                voucherItemData1.setKmmc(ccname1);
                voucherItemData1.setJf(je1);
                voucherItemDatas.add(voucherItemData1);
            }
            if(je2 !=0){
                VoucherItemData voucherItemData2 = new VoucherItemData();
                voucherItemData2.setZy("转存"+xyz.getYfczyh().getQc()+"保证金");
                voucherItemData2.setKmdm(ccode2);
                voucherItemData2.setKmmc(ccname2);
                voucherItemData2.setDf(je2);
                voucherItemDatas.add(voucherItemData2);
            }
            if(je3 !=0){//有辅助核算
                VoucherItemData voucherItemData3 = new VoucherItemData();
                voucherItemData3.setZy("付"+xyz.getYfczyh().getQc()+"信用证开证费用");
                voucherItemData3.setKmdm(ccode3);
                voucherItemData3.setKmmc(ccname3);
                voucherItemData3.setJf(je3);
                //todo 暂时这么使用财务部
                voucherItemData3.setDeptCode("0401");
                voucherItemData3.setDeptName("通用部门辅助核算代码");
                voucherItemData3.setSupplierName(xyz.getYfczyh().getJc());
                voucherItemData3.setSupplierCode(xyz.getYfczyh().getGysfzhsdm());
                voucherItemDatas.add(voucherItemData3);
            }
            if(je4 !=0){
                VoucherItemData voucherItemData4 = new VoucherItemData();
                voucherItemData4.setZy("付"+xyz.getYfczyh().getQc()+"信用证开证费用");
                voucherItemData4.setKmdm(ccode4);
                voucherItemData4.setKmmc(ccname4);
                voucherItemData4.setDf(je4);
                voucherItemDatas.add(voucherItemData4);
            }
        }else{
            //信用证的还款----我方付息
            if(xyz.getStatus()==2 && xyz.getSfwffx()==0){
                List<Khzhsfmx> khzhsfmxes  = getKhhzfmx(xyz.getId());
                Double je2 = 0d;
                for(Khzhsfmx k :khzhsfmxes){
                    if(k.getGllx()==3){
                        je2 = k.getJe();
                    }
                }
                String ccode3 =khzhsfmxes.get(0).getRzyh().getKmdm()+"";//这里应该是开户行的jiuqicode
                String ccname3 ="银行存款/"+khzhsfmxes.get(0).getRzyh().getQc();
                //支付方式需要带入
                Cykmdmys   cykmdmys2 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"2700");
                String ccname2 =cykmdmys2.getJqcwkmmc();
                String ccode2 =cykmdmys2.getKmdm();
                //todo
                Cykmdmys   cykmdmys1 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"3000");
                String ccname1 =cykmdmys1.getJqcwkmmc();
                String ccode1 =cykmdmys1.getKmdm();
                Double je1 = Double.parseDouble(xyz.getDqje().toString());
                Double je3 = Double.parseDouble(xyz.getDqje().toString())+je2;
                List<VoucherItemData> voucherItemDataList = new ArrayList<VoucherItemData>();
                if(je1 !=0){
                    VoucherItemData voucherItemData1 = new VoucherItemData();
                    voucherItemData1.setZy("付"+khzhsfmxes.get(0).getRzyh().getQc()+"到期信用证款");
                    voucherItemData1.setKmdm(ccode1);
                    voucherItemData1.setKmmc(ccname1);
                    voucherItemData1.setJf(je1);
                    String fzhs1 =getFzhsByCode(xyz.getNbgs().getId(),ccode1.toString());
                    voucherItemData1 = getFzhsMxByFzsh(fzhs1,voucherItemData1,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData1);
                }
                if(je2 !=0){
                    VoucherItemData voucherItemData2 = new VoucherItemData();
                    voucherItemData2.setZy("付"+khzhsfmxes.get(0).getRzyh().getQc()+"信用证利息支出");
                    voucherItemData2.setKmdm(ccode2);
                    voucherItemData2.setKmmc(ccname2);
                    voucherItemData2.setJf(je2);
                    String fzhs2 =getFzhsByCode(xyz.getNbgs().getId(),ccode2.toString());
                    voucherItemData2 = getFzhsMxByFzsh(fzhs2,voucherItemData2,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData2);
                }
                if(je3 !=0){
                    VoucherItemData voucherItemData3 = new VoucherItemData();
                    voucherItemData3.setZy("付"+khzhsfmxes.get(0).getRzyh().getQc()+"到期信用证款");
                    voucherItemData3.setKmdm(ccode3);
                    voucherItemData3.setKmmc(ccname3);
                    voucherItemData3.setDf(je3);
                    voucherItemDatas.add(voucherItemData3);
                }
            }

            //信用证的还款----他方付息
            if(xyz.getStatus()==2 && xyz.getSfwffx()==1){
                Cykmdmys cykmdmys =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"300");
                String ccode1 =cykmdmys.getKmdm()+"";//这里应该是开户行的jiuqicode
                String ccname1 =cykmdmys.getJqcwkmmc();
                //todo
                Cykmdmys   cykmdmys2 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"3000");
                String ccname2 =cykmdmys2.getJqcwkmmc();
                String ccode2 =cykmdmys2.getKmdm();
                Double je1 = Double.parseDouble(xyz.getDqje().toString());
                Double je2 = Double.parseDouble(xyz.getDqje().toString());
                List<VoucherItemData> voucherItemDataList = new ArrayList<VoucherItemData>();
                if(je1 !=0){
                    VoucherItemData voucherItemData1 = new VoucherItemData();
                    voucherItemData1.setZy("付"+xyz.getSfk().getGldw().getJc()+"款");
                    voucherItemData1.setKmdm(ccode1);
                    voucherItemData1.setKmmc(ccname1);
                    voucherItemData1.setJf(je1);
//                String fzhs1 =getFzhsByCode(xyz.getNbgs().getId(),ccode1.toString());
//                voucherItemData1 = getFzhsMxByFzsh(fzhs1,voucherItemData1,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData1);
                }
                if(je2 !=0){
                    VoucherItemData voucherItemData2 = new VoucherItemData();
                    voucherItemData2.setZy("付"+xyz.getSfk().getGldw().getJc()+"款");
                    voucherItemData2.setKmdm(ccode2);
                    voucherItemData2.setKmmc(ccname2);
                    voucherItemData2.setDf(je2);
                    //辅助核算暂时没看见
//                String fzhs2 =getFzhsByCode(xyz.getNbgs().getId(),ccode2.toString());
//                voucherItemData2 = getFzhsMxByFzsh(fzhs2,voucherItemData2,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData2);
                }
            }


            //信用证变现我方付息
            if(xyz.getStatus()==3 && xyz.getSfwffx()==0){
                List<Khzhsfmx> khzhsfmxes  = getKhhzfmx(xyz.getId());
                Double je2 = 0d;
                for(Khzhsfmx k :khzhsfmxes){
                    if(k.getGllx()==3){
                        je2 = k.getJe();
                    }
                }
                String ccode1 =khzhsfmxes.get(0).getRzyh().getKmdm()+"";//这里应该是开户行的jiuqicode
                String ccname1 ="银行存款/"+khzhsfmxes.get(0).getRzyh().getQc();
                //支付方式需要带入
                Cykmdmys   cykmdmys2 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"2700");
                String ccname2 =cykmdmys2.getJqcwkmmc();
                String ccode2 =cykmdmys2.getKmdm();
                //todo
                Cykmdmys   cykmdmys3 =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"3000");
                String ccname3 =cykmdmys3.getJqcwkmmc();
                String ccode3 =cykmdmys3.getKmdm();
                Double je1 = Double.parseDouble(xyz.getDqje().toString())-je2;
                Double je3 = Double.parseDouble(xyz.getDqje().toString());
                List<VoucherItemData> voucherItemDataList = new ArrayList<VoucherItemData>();
                if(je1 !=0){
                    VoucherItemData voucherItemData1 = new VoucherItemData();
                    voucherItemData1.setZy("收"+khzhsfmxes.get(0).getRzyh().getQc()+"到期信用证款");
                    voucherItemData1.setKmdm(ccode1);
                    voucherItemData1.setKmmc(ccname1);
                    voucherItemData1.setJf(je1);
                    voucherItemDatas.add(voucherItemData1);
                }
                if(je2 !=0){
                    VoucherItemData voucherItemData2 = new VoucherItemData();
                    voucherItemData2.setZy("收"+khzhsfmxes.get(0).getRzyh().getQc()+"到期信用证款");
                    voucherItemData2.setKmdm(ccode2);
                    voucherItemData2.setKmmc(ccname2);
                    voucherItemData2.setJf(je2);
                    String fzhs2 =getFzhsByCode(xyz.getNbgs().getId(),ccode2.toString());
                    voucherItemData2 = getFzhsMxByFzsh(fzhs2,voucherItemData2,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData2);
                }
                if(je3 !=0){
                    VoucherItemData voucherItemData3 = new VoucherItemData();
                    voucherItemData3.setZy("收"+khzhsfmxes.get(0).getRzyh().getQc()+"到期信用证款");
                    voucherItemData3.setKmdm(ccode3);
                    voucherItemData3.setKmmc(ccname3);
                    voucherItemData3.setDf(je3);
                    String fzhs3 =getFzhsByCode(xyz.getNbgs().getId(),ccode3.toString());
                    voucherItemData3 = getFzhsMxByFzsh(fzhs3,voucherItemData3,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData3);
                }
            }

            //信用证的变现----他方付息
            if(xyz.getStatus()==3 && xyz.getSfwffx()==1){
                Cykmdmys cykmdmys =this.cykmdmysDao.getCommentSubject(xyz.getNbgs().getId(),"3000");
                String ccode2 =cykmdmys.getKmdm()+"";//这里应该是开户行的jiuqicode
                String ccname2 =cykmdmys.getJqcwkmmc();
                //todo
                List<Khzhsfmx> khzhsfmxes  = getKhhzfmx(xyz.getId());
                String ccode1 =khzhsfmxes.get(0).getRzyh().getKmdm()+"";//这里应该是开户行的jiuqicode
                String ccname1 ="银行存款/"+khzhsfmxes.get(0).getRzyh().getQc();
                Double je1 = Double.parseDouble(xyz.getDqje().toString());
                Double je2 = Double.parseDouble(xyz.getDqje().toString());
                List<VoucherItemData> voucherItemDataList = new ArrayList<VoucherItemData>();
                if(je1 !=0){
                    VoucherItemData voucherItemData1 = new VoucherItemData();
                    voucherItemData1.setZy("收"+xyz.getSfk().getGldw().getJc()+"到期信用证款");
                    voucherItemData1.setKmdm(ccode1);
                    voucherItemData1.setKmmc(ccname1);
                    voucherItemData1.setJf(je1);
//                String fzhs1 =getFzhsByCode(xyz.getNbgs().getId(),ccode1.toString());
//                voucherItemData1 = getFzhsMxByFzsh(fzhs1,voucherItemData1,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData1);
                }
                if(je2 !=0){
                    VoucherItemData voucherItemData2 = new VoucherItemData();
                    voucherItemData2.setZy("收"+xyz.getSfk().getGldw().getJc()+"到期信用证款");
                    voucherItemData2.setKmdm(ccode2);
                    voucherItemData2.setKmmc(ccname2);
                    voucherItemData2.setDf(je2);
                    //辅助核算暂时没看见
                    String fzhs2 =getFzhsByCode(xyz.getNbgs().getId(),ccode2.toString());
                    voucherItemData2 = getFzhsMxByFzsh(fzhs2,voucherItemData2,khzhsfmxes);
                    voucherItemDatas.add(voucherItemData2);
                }
            }
        }
        return voucherItemDatas;
    }

    public String getFzhsByCode(Long gsId,String code){
        return this.kmdmService.findKmdmDetailByGs(gsId,code).getFzhslx();
    }

    public VoucherItemData getFzhsMxByFzsh(String fzhs,VoucherItemData voucherItemData,List<Khzhsfmx> khzhsfmxes){
        fzhs = fzhs.replaceAll(" ","");
        String[] fzhss = fzhs.split("；");
        if(fzhss.length>0){
            for(String f :fzhss){
                if(f.equals("供应商")){
                    voucherItemData.setSupplierCode(khzhsfmxes.get(0).getRzyh().getGysfzhsdm());
                    voucherItemData.setSupplierName(khzhsfmxes.get(0).getRzyh().getJc());
                }
            }
        }
        return voucherItemData;
    }

    public Integer praseLongToIntegr(Long l){
        return Integer.parseInt(l.toString());
    }
    @Autowired
    public void setKmdmService(KmdmService kmdmService) {
        this.kmdmService = kmdmService;
    }
    @Autowired
    public void setCykmdmysDao(CykmdmysDao cykmdmysDao) {
        this.cykmdmysDao = cykmdmysDao;
    }
}
